﻿using System;
using System.Collections.Generic;
using System.Linq;
using yiCommerce.Core;
using yiCommerce.Core.Domain.Products;
using yiCommerce.Models.Products;
using yiCommerce.Service.Pictures;

namespace yiCommerce.Mapper
{
    public class ProductMapper
    {

        public static IEnumerable<ProductOverviewModel> PrepareProductOverviewModels(IEnumerable<Product> products)
        {
            var _pictureService = EngineContext.Current.Resolve<IPictureService>();

            var models = new List<ProductOverviewModel>();
            foreach (var product in products)
            {
                var model = new ProductOverviewModel()
                {
                    Id = product.Id,
                    Name = product.ProductName,
                    ShortDescription = product.ShortDescription,
                    FullDescription = product.FullDescription,
                };
                model.MinPrice = product.ProductVariants.Min(c => c.Price).ToString("G2");



                //default picture
                model.DefaultPictureModel = new PictureModel()
                {
                    ImageUrl = _pictureService.GetPictureUrl(product.ProductPictures.FirstOrDefault().Picture.Id, 300, false),
                    FullSizeImageUrl = _pictureService.GetPictureUrl(product.ProductPictures.FirstOrDefault().Picture.Id, 0, false),
                    Title = model.Name,
                };

                model.SpecificationAttributeModels = PrepareProductSpecificationModel(product);

                models.Add(model);

            }
            return models;
        }



        protected static IList<ProductSpecificationAttributeModel> PrepareProductSpecificationModel(Product product)
        {
            if (product == null)
                throw new ArgumentNullException("product");

            var model = product.ProductSpecificationAttributes
                    .Select(psa =>
                    {
                        return new ProductSpecificationAttributeModel()
                        {
                            AttributeName = psa.SpecificationAttributeOption.SpecificationAttribute.Name,
                            AttributeOptionName = psa.SpecificationAttributeOption.Name
                        };
                    }).ToList();
            return model;
        }
    }
}